<template>
	<view class="container">
		<view class="carousel">
			<swiper indicator-dots circular=true duration="400">
				<swiper-item class="swiper-item" v-for="(item,index) in imgList" :key="index">
					<view class="image-wrapper">
						<image :src="item" class="loaded" mode="aspectFill"></image>
					</view>
				</swiper-item>
			</swiper>
		</view>
		
		<view class="introduce-section">
			<text class="title">{{info.name}}</text>
			<view class="price-box">
				<text class="price-tip">￥</text>
				<text class="price">{{info.retail_price}}</text>
				<text class="m-price">￥{{info.market_price}}</text>
				<text class="coupon-tip" v-if="info.is_active">{{info.active}}</text>
			</view>
			<view class="bot-row">
				<text>销量: {{info.sales_volume + info.preset_num}}</text>
				<text>库存: {{info.stock}}</text>
				<text>浏览量: {{info.look_number||'999+'}}</text>
			</view>
		</view>
		
		<!--  分享 -->
		<view class="share-section"> <!-- @click="share"> -->
			<view class="share-icon">
				<text class="yticon icon-xingxing"></text>
				 <!-- 返 -->
			</view>
			<text class="tit">{{info.share_intro}}</text>
			<text class="yticon icon-bangzhu1"></text>
			<!-- <view class="share-btn">
				立即分享
				<text class="yticon icon-you"></text>
			</view> -->
			
		</view>
		
		<view class="c-list">
			<view class="c-row b-b" @click="toggleSpec">
				<text class="tit">购买类型</text>
				<view class="con">
					<text class="selected-text" v-for="(sItem, sIndex) in specSelected" :key="sIndex">
						{{sItem.title}}
					</text>
				</view>
				<text class="yticon icon-you"></text>
			</view>
			<!-- <view class="c-row b-b">
				<text class="tit">优惠券</text>
				<text class="con t-r red" @click="receive">领取优惠券</text>
				<text class="yticon icon-you"></text>
			</view>
			<view class="c-row b-b">
				<text class="tit">促销活动</text>
				<view class="con-list">
					<text>新人首单送20元无门槛代金券</text>
					<text>订单满50减10</text>
					<text>订单满100减30</text>
					<text>单笔购买满两件免邮费</text>
				</view>
			</view> -->
			<view class="c-row b-b">
				<text class="tit">服务</text>
				<view class="bz-list con">
					<text v-for="(m, i) in goodsService">
						{{m.name}} ·
					</text>
				</view>
			</view>
		</view>
		
		<!-- 评价 -->
		<view class="eva-section">
			<view class="e-header">
				<text class="tit">评价</text>
				<text>({{info.common_num||0}})</text>
				<text class="tip">好评率 {{info.common_rate||100}}%</text>
				<text class="yticon icon-you"></text>
			</view> 
			<view class="eva-box" v-if="info.common">
				<image class="portrait" :src="info.common.user.head_img" mode="aspectFill"></image>
				<view class="right">
					<text class="name">{{info.common.user.nickname}}</text>
					<text class="con">{{info.common.content}}</text>
					<!-- <view class="bot">
						<text class="attr">购买类型：XL 红色</text>
						<text class="time">2019-04-01 19:21</text>
					</view> -->
				</view>
			</view>
		</view>
		
		<view class="detail-desc" style="text-align: center;">
			<view class="d-header">
				<text>图文详情</text>
			</view>
			<mp-html :content="desc" />
		</view>
		
		
		<!-- 底部操作菜单 -->
		<view class="page-bottom">
			<navigator url="/pages/index/index" open-type="switchTab" class="p-b-btn">
				<text class="yticon icon-xiatubiao--copy"></text>
				<text>首页</text>
			</navigator>
			<navigator url="/pages/cart/cart" open-type="switchTab" class="p-b-btn">
				<text class="yticon icon-gouwuche"></text>
				<text>购物车</text>
			</navigator>
			 <view class="p-b-btn" :class="{active: favorite}" @click="toFavorite">
				<text class="iconfont icon-dianzan1"></text>
				<text>收藏</text>
			</view>
			
			<view class="action-btn-group">
				<button type="primary" class=" action-btn no-border buy-now-btn" @click="buy">立即购买</button>
				<button type="primary" class=" action-btn no-border add-cart-btn" @click="toCart">加入购物车</button>
			</view>
		</view>
		
		
		<!-- 规格-模态层弹窗 -->
		<view class="popup spec" :class="specClass" @touchmove.stop.prevent="stopPrevent" @click="toggleSpec" >
			<!-- 遮罩层 -->
			<view class="mask"></view>
			<view class="layer attr-content" @click.stop="stopPrevent">
				<view class="a-t">
					<image :src="info.img"></image>
					<view class="right">
						<view  v-if="info.spec_type">
							<text class="price" >￥{{defaultProduct.retail_price}}</text>
							<text class="stock">库存：{{defaultProduct.stock}}件</text>
							
							<view class="selected">
								已选：<text class="selected-text">{{defaultProduct.spec_name}}</text>
							</view>
						</view>
						<view v-else>
							<text class="price">￥ {{info.retail_price}}</text>
							<text class="stock">库存：{{info.stock}}件</text>
						</view>
					</view>
				</view>
				<view v-for="(item,index) in specList" :key="index" class="attr-list" v-if="info.spec_type">
					<text>{{item.title}}</text>
					<view class="item-list">
						<text v-for="(childItem, childIndex) in specChildList" v-if="childItem.base_spec_id == item.base_spec_id"  :key="childIndex" class="tit" 
						:class="{selected: childItem.selected}" @click="selectSpec(childIndex, childItem.id)" >
							{{childItem.title}}
						</text>
					</view>
				</view>
				
				<!-- 数量 -->
				<view class="specs bugNum">
					<view class="spTIt">购买数量</view>
					<view class="spList">
						<uni-number-box :min="1" :value="numberValue" @eventChange="numberChange" 
						style="right: 0;text-align: right;left:calc(100% - 230rpx);"/>
					</view>
				</view>
				<view class="action-btn-group">
					<button  class=" btn" style="width: 45%;float: left;" @click="buy">立即购买</button>
					<button  class=" btn" style="width: 45%;float: right;" @click="toCart">加入购物车</button>
				</view>
				<!-- <button class="btn" @click="toggleSpec">完成</button> -->
			</view>
		</view>
		<!-- 分享 -->
		<!-- <share ref="share" :contentHeight="580" :shareList="shareList"></share> -->
	</view>
</template>

<script>
	//import share from '@/components/share';
	import * as marked from 'marked'
	import uniNumberBox from '../../../components/uni-number-box.vue'
	import {
	    mapState ,mapMutations
	} from 'vuex'; 
	export default{
		components: {
			//share,
			uniNumberBox,
		},
		data() {
			return {
				numberValue:1,
				specClass: 'none',
				specSelected:[],
				info:{},
				goods_id:0,
				product_id:0,
				favorite: true,
				shareList: [],
				imgList: [],
				goodsService:[],
				product:[],
				defaultProduct:{},
				desc: `暂无内容`,
				specList: [],
				specChildList: [],
				cimgs:[]
			};
		},
		async onLoad(options){
			//接收传值,id里面放的是标题，因为测试数据并没写id 
			let id = options.id;
			this.goods_id = id;
			let _param = {
				id:id,
				token:this.userInfo.token
			};
			let that = this;
			this.$api.getUrl(this.$api._api.ApiGoodsDetail, _param, function(res){
				
				that.info = res.info;
				that.favorite = res.is_collect == 1 ? true : false;
				that.imgList = res.info.images;
				that.desc = marked.marked(res.detail.details);
				//that.cimgs = _data.data.cimgs
				that.specList = res.goods_spec
				that.specChildList = res.goods_spec_val
				that.goodsService = res.service
				that.product = res.product
				
				that.defaultProduct  = {
					goods_id:res.info.id,
					name:res.info.name,
					thumb_img:res.info.img,
					retail_price:res.info.retail_price,
					stock:res.info.stock,
				}
				// var _obj = {
				// 	goods_id:res.info.id,
				// 	goods_name:res.info.name,
				// 	thumb_img:res.info.img
				// }
				
				
				// var _browse = uni.getStorageSync("browse")
				// if(!_browse){_browse = [];}
				// for(var i=0;i<_browse.length;i++){
				// 	if(_browse[i].goods_id == _data.data.goods_id){
				// 		_browse.splice(i,1)
				// 	}
				// }
				// _browse.splice(0,0,_obj)
				// uni.setStorageSync("browse",_browse)
				
				//规格 默认选中第一条
				let select_base_id = [];
				for(let cItem of that.specChildList){
					if(cItem ){
						let _index = select_base_id.findIndex(re=>re==cItem.base_spec_id)
						if(_index == -1){
							that.$set(cItem, 'selected', true);
							that.specSelected.push(cItem);
							select_base_id.push(cItem.base_spec_id)
						}
					}
				}
				
				// 初始默认规格数据
				res.info.spec_type && that.initProduct()
			})
			
			//this.shareList = await this.$api.json('shareList');			
		},
		computed: {
			...mapState(['hasLogin','userInfo'])
		},
		methods:{
			...mapMutations(["login"]),
			
			initProduct:function(){
				if(this.specList.length != this.specSelected.length) return ;
				
				let _ids = [];
				this.specSelected.sort((a, b)=>a.base_spec_value_id - b.base_spec_value_id)
				this.specSelected.map(re=>{
					_ids.push(re.base_spec_value_id)
				})
				
				let default_select = _ids.join('-')

				this.product.map(re=>{
					if(re.spec_ids == default_select){
						this.defaultProduct= re;
					}
				})

			},
			
			//规格弹窗开关
			toggleSpec() {
				if(this.specClass === 'show'){
					this.specClass = 'hide';
					setTimeout(() => {
						this.specClass = 'none';
					}, 250);
				}else if(this.specClass === 'none'){
					this.specClass = 'show';
				}
			},
			//选择规格
			selectSpec(index, id){
				let list = this.specChildList;
				
				list.forEach(item=>{
					if(item.base_spec_id == list[index].base_spec_id) {this.$set(item, 'selected', false);}
				})

				this.$set(list[index], 'selected', true);
				this.specChildList = list;
				
				//存储已选择
				/**
				 * 修复选择规格存储错误
				 * 将这几行代码替换即可
				 * 选择的规格存放在specSelected中
				 */
				this.specSelected = []; 
				list.forEach(item=>{ 
					if(item.selected === true){ 
						this.specSelected.push(item); 
					} 
				})
				
				this.initProduct();
			},
			//数量
			numberChange(value) {
				this.numberValue = value.number;
			},
			//分享
			share(){
				this.$refs.share.toggleMask();	
			},
			//收藏
			toFavorite(){
				this.favorite = !this.favorite;
				let _message = this.favorite ? '收藏成功' : '取消成功'
				let that = this;
				let _param = {
					goods_id:this.goods_id,
					token:this.userInfo.token
				};
				
				this.$api.postUrl(this.$api._api.ApiGoodsCollect,_param,function(res){
					that.$api.msg(_message);
				})
			},
			buy(){
				if(!this.hasLogin){
					this.$api.msg("请先登陆");
					return false;
				}
				let _attr_id = this.defaultProduct.id ||  0;
				let that = this;
				if(this.specChildList.length > 0){
					if(this.specSelected.length < 1){
						this.$api.msg("请选择规格");
					}
				}
				
				uni.navigateTo({
					url: '/pages/user/order/buy?goods_id='+this.goods_id+'&product_id='+_attr_id+'&goods_number='+this.numberValue
				})
			},
			toCart:function(){
				let _attr_id = 0;
				let that = this;
				if(that.specChildList.length > 0){
					if(that.specSelected.length < 1){
						that.$api.msg("请选择规格");
					}
					_attr_id = that.specSelected[0].attr_id;
				}
				if(that.userInfo.length < 1){
					that.$api.msg("请登陆..");
					uni.navigateTo({url:'/pages/public/login'})  
					return false;
				} else{
					// 临时测试token
					// this.$set(this.userInfo,"token","d4123404e2e78c00802e27007252d723");
				}
				let _param = {
					goods_id:that.goods_id,
					product_id:that.defaultProduct.id || 0,
					number:that.numberValue,
					token:that.userInfo.token
				};
				
				that.$api.postUrl(that.$api._api.ApiCartAdd,_param,function(res){
					
					if(res){
						that.$api.msg("操作成功");
						that.specClass = 'none';
						let _info = that.userInfo;
						let _num = (isNaN(that.userInfo.cart_number) ? 0 : parseInt(that.userInfo.cart_number))+that.numberValue;
						that.$set(_info, "cart_number", _num);
						that.login(_info);
						
						that.$api.setCartNumber();
					}else{
						that.$api.msg("操作失败");
					}
				})
			},
			stopPrevent(){},
			//领取优惠券
			// receive(){
			// 	var that = this
			// 	if(that.userInfo.token && that.userInfo.token != 'undefined'){
			// 		var token = that.userInfo.token
			// 		var _param = {
			// 			token:token,
			// 		}
			// 		that.$api.postUrl(that.$api._api.ApiCouponReceive,_param,function(res){
			// 			let _data = {}
			// 			if(typeof(res.data) == "object"){
			// 				_data = res.data
			// 			}else{
			// 				_data = JSON.parse(res.data)
			// 			}
			// 			that.$api.msg(_data.message)
			// 		})
			// 	}else{
			// 		uni.navigateTo({
			// 			url:'/pages/public/login'
			// 		})
			// 	}
			// }
		},

	}
</script>

<style lang='scss'>
	page{
		background: $page-color-base;
		padding-bottom: 160rpx;
	}
	.bugNum{display: flex;align-items: center;height: 100rpx;position: relative;}
	.bugNum .spTIt{margin-bottom: 0;flex: 1;width:200rpx;}
	.bugNum .spList{text-align: right;background: red;}
	
	/* .specs .spList {flex: 1;display: flex;flex-wrap: wrap;} */
	.spList .num {height: 56rpx;text-align: center;line-height: 56rpx;font-size: 28rpx;color: #666;padding: 0 20rpx;margin-right: 20rpx;margin-bottom: 20rpx;}
	.spList .curr {background: #19294f;color: #edaf1a;}
	.spList .currRab{background: #eee;color: #cac5c5;}
	.icon-you{
		font-size: $font-base + 2rpx;
		color: #888;
	}
	.carousel {
		height: 722rpx;
		position:relative;
		swiper{
			height: 100%;
		}
		.image-wrapper{
			width: 100%;
			height: 100%;
		}
		.swiper-item {
			display: flex;
			justify-content: center;
			align-content: center;
			height: 750rpx;
			overflow: hidden;
			image {
				width: 100%;
				height: 100%;
			}
		}
		
	}
	
	/* 标题简介 */
	.introduce-section{
		background: #fff;
		padding: 20rpx 30rpx;
		
		.title{
			font-size: 32rpx;
			color: $font-color-dark;
			height: 50rpx;
			line-height: 50rpx;
		}
		.price-box{
			display:flex;
			align-items:baseline;
			height: 64rpx;
			padding: 10rpx 0;
			font-size: 26rpx;
			color:$uni-color-primary;
		}
		.price{
			font-size: $font-lg + 2rpx;
		}
		.m-price{
			margin:0 12rpx;
			color: $font-color-light;
			text-decoration: line-through;
		}
		.coupon-tip{
			align-items: center;
			padding: 4rpx 10rpx;
			background: $uni-color-primary;
			font-size: $font-sm;
			color: #fff;
			border-radius: 6rpx;
			line-height: 1;
			transform: translateY(-4rpx); 
		}
		.bot-row{
			display:flex;
			align-items:center;
			height: 50rpx;
			font-size: $font-sm;
			color: $font-color-light;
			text{
				flex: 1;
			}
		}
	}
	/* 分享 */
	.share-section{
		display:flex;
		align-items:center;
		color: $font-color-base;
		background: linear-gradient(left, #fdf5f6, #fbebf6);
		padding: 12rpx 30rpx;
		.share-icon{
			display:flex;
			align-items:center;
			width: 70rpx;
			height: 30rpx;
			line-height: 1;
			border: 1px solid $uni-color-primary;
			border-radius: 4rpx;
			position:relative;
			overflow: hidden;
			font-size: 22rpx;
			color: $uni-color-primary;
			&:after{
				content: '';
				width: 50rpx;
				height: 50rpx;
				border-radius: 50%;
				left: -20rpx;
				top: -12rpx;
				position:absolute;
				background: $uni-color-primary;
			}
		}
		.icon-xingxing{
			position:relative;
			z-index: 1;
			font-size: 24rpx;
			margin-left: 2rpx;
			margin-right: 10rpx;
			color: #fff;
			line-height: 1;
		}
		.tit{
			font-size: $font-base;
			margin-left:10rpx;
		}
		.icon-bangzhu1{
			padding: 10rpx;
			font-size: 30rpx;
			line-height: 1;
		}
		.share-btn{
			flex: 1;
			text-align:right;
			font-size: $font-sm;
			color: $uni-color-primary;
		}
		.icon-you{
			font-size: $font-sm;
			margin-left: 4rpx;
			color: $uni-color-primary;
		}
	}
	
	.c-list{
		font-size: $font-sm + 2rpx;
		color: $font-color-base;
		background: #fff;
		.c-row{
			display:flex;
			align-items:center;
			padding: 20rpx 30rpx;
			position:relative;
		}
		.tit{
			width: 140rpx;
		}
		.con{
			flex: 1;
			color: $font-color-dark;
			.selected-text{
				margin-right: 10rpx;
			}
		}
		.bz-list{
			height: 40rpx;
			font-size: $font-sm+2rpx;
			color: $font-color-dark;
			text{
				display: inline-block;
				margin-right: 30rpx;
			}
		}
		.con-list{
			flex: 1;
			display:flex;
			flex-direction: column;
			color: $font-color-dark;
			line-height: 40rpx;
		}
		.red{
			color: $uni-color-primary;
		}
	}
	
	/* 评价 */
	.eva-section{
		display: flex;
		flex-direction: column;
		padding: 20rpx 30rpx;
		background: #fff;
		margin-top: 16rpx;
		.e-header{
			display: flex;
			align-items: center;
			height: 70rpx;
			font-size: $font-sm + 2rpx;
			color: $font-color-light;
			.tit{
				font-size: $font-base + 2rpx;
				color: $font-color-dark;
				margin-right: 4rpx;
			}
			.tip{
				flex: 1;
				text-align: right;
			}
			.icon-you{
				margin-left: 10rpx;
			}
		}
	}
	.eva-box{
		display: flex;
		padding: 20rpx 0;
		.portrait{
			flex-shrink: 0;
			width: 80rpx;
			height: 80rpx;
			border-radius: 100px;
		}
		.right{
			flex: 1;
			display: flex;
			flex-direction: column;
			font-size: $font-base;
			color: $font-color-base;
			padding-left: 26rpx;
			.con{
				font-size: $font-base;
				color: $font-color-dark;
				padding: 20rpx 0;
			}
			.bot{
				display: flex;
				justify-content: space-between;
				font-size: $font-sm;
				color:$font-color-light;
			}
		}
	}
	/*  详情 */
	.detail-desc{
		background: #fff;
		margin-top: 16rpx;
		.d-header{
			display: flex;
			justify-content: center;
			align-items: center;
			height: 80rpx;
			font-size: $font-base + 2rpx;
			color: $font-color-dark;
			position: relative;
				
			text{
				padding: 0 20rpx;
				background: #fff;
				position: relative;
				z-index: 1;
			}
			&:after{
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translateX(-50%);
				width: 300rpx;
				height: 0;
				content: '';
				border-bottom: 1px solid #ccc; 
			}
		}
	}
	
	/* 规格选择弹窗 */
	.attr-content{
		padding: 10rpx 30rpx;
		.a-t{
			display: flex;
			image{
				width: 170rpx;
				height: 170rpx;
				flex-shrink: 0;
				margin-top: -40rpx;
				border-radius: 8rpx;;
			}
			.right{
				display: flex;
				flex-direction: column;
				padding-left: 24rpx;
				font-size: $font-sm + 2rpx;
				color: $font-color-base;
				line-height: 42rpx;
				.price{
					font-size: $font-lg;
					color: $uni-color-primary;
					margin-bottom: 10rpx;
					display: block;
				}
				.selected-text{
					margin-right: 10rpx;
				}
			}
		}
		.attr-list{
			display: flex;
			flex-direction: column;
			font-size: $font-base + 2rpx;
			color: $font-color-base;
			padding-top: 30rpx;
			padding-left: 10rpx;
		}
		.item-list{
			padding: 20rpx 0 0;
			display: flex;
			flex-wrap: wrap;
			text{
				display: flex;
				align-items: center;
				justify-content: center;
				background: #eee;
				margin-right: 20rpx;
				margin-bottom: 20rpx;
				/* border-radius: 100rpx; */
				min-width: 60rpx;
				height: 60rpx;
				padding: 0 20rpx;
				font-size: $font-base;
				color: $font-color-dark;
				border:1px solid #fff
			}
			.selected{
				background: #fbebee;
				color: $uni-color-primary;
				border: 1px solid #e4d3d6;
			}
		}
	}
	
	/*  弹出层 */
	.popup {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 99;
		
		&.show {
			display: block;
			.mask{
				animation: showPopup 0.2s linear both;
			}
			.layer {
				animation: showLayer 0.2s linear both;
			}
		}
		&.hide {
			.mask{
				animation: hidePopup 0.2s linear both;
			}
			.layer {
				animation: hideLayer 0.2s linear both;
			}
		}
		&.none {
			display: none;
		}
		.mask{
			position: fixed;
			top: 0;
			width: 100%;
			height: 100%;
			z-index: 1;
			background-color: rgba(0, 0, 0, 0.4);
		}
		.layer {
			position: fixed;
			z-index: 99;
			bottom: 0;
			width: 100%;
			min-height: 40vh;
			border-radius: 10rpx 10rpx 0 0;
			background-color: #fff;
			.btn{
				height: 66rpx;
				line-height: 66rpx;
				border-radius: 100rpx;
				background: $uni-color-primary;
				font-size: $font-base + 2rpx;
				color: #fff;
				margin: 30rpx auto 20rpx;
			}
		}
		@keyframes showPopup {
			0% {
				opacity: 0;
			}
			100% {
				opacity: 1;
			}
		}
		@keyframes hidePopup {
			0% {
				opacity: 1;
			}
			100% {
				opacity: 0;
			}
		}
		@keyframes showLayer {
			0% {
				transform: translateY(120%);
			}
			100% {
				transform: translateY(0%);
			}
		}
		@keyframes hideLayer {
			0% {
				transform: translateY(0);
			}
			100% {
				transform: translateY(120%);
			}
		}
	}
	
	/* 底部操作菜单 */
	.page-bottom{
		position:fixed;
		left: 30rpx;
		bottom:30rpx;
		z-index: 95;
		display: flex;
		justify-content: space-around;
		align-items: center;
		width: 690rpx;
		height: 100rpx;
		background: rgba(255,255,255,.9);
		box-shadow: 0 0 20rpx 0 rgba(0,0,0,.5);
		border-radius: 16rpx;
		
		.p-b-btn{
			display:flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: $font-sm;
			color: $font-color-base;
			width: 96rpx;
			height: 80rpx;
			.iconfont{
				font-size:30rpx;
				line-height: 48rpx;
				color: $font-color-light;
			}
			&.active, &.active .iconfont{
				color: $uni-color-primary;
			}
			.icon-fenxiang2{
				font-size: 42rpx;
				transform: translateY(-2rpx);
			}
			.icon-shoucang{
				font-size: 46rpx;
			}
		}
		.action-btn-group{
			display: flex;
			height: 76rpx;
			border-radius: 100px;
			overflow: hidden;
			box-shadow: 0 20rpx 40rpx -16rpx #fa436a;
			box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
			background: linear-gradient(to right, #ffac30,#fa436a,#F56C6C);
			margin-left: 20rpx;
			position:relative;
			&:after{
				content: '';
				position:absolute;
				top: 50%;
				right: 50%;
				transform: translateY(-50%);
				height: 28rpx;
				width: 0;
				border-right: 1px solid rgba(255,255,255,.5);
			}
			.action-btn{
				display:flex;
				align-items: center;
				justify-content: center;
				width: 180rpx;
				height: 100%;
				font-size: $font-base ;
				padding: 0;
				border-radius: 0;
				background: transparent;
			}
		}
	}


	.uni-numbox{position: unset;}
</style>
